package test.xml;

import java.io.File;
import java.io.FileOutputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class ModifyXml {
	
	private static Node selectSingleNode(String express, Object source) throws Exception  {//查找节点，并返回第一个符合条件节点
		Node result=null;
		XPathFactory xpathFactory=XPathFactory.newInstance();
		XPath xpath=xpathFactory.newXPath();
		result=(Node) xpath.evaluate(express, source, XPathConstants.NODE);
		return result;
	}
	
	private static NodeList selectNodes(String express, Object source) throws Exception  {//查找节点，返回符合条件的节点集。
		NodeList result=null;
		XPathFactory xpathFactory=XPathFactory.newInstance();
		XPath xpath=xpathFactory.newXPath();
		result=(NodeList) xpath.evaluate(express, source, XPathConstants.NODESET);
		return result;
	}
	
	private static void saveXml(String fileName, Document doc) throws Exception {//将Document输出到文件
		TransformerFactory transFactory=TransformerFactory.newInstance();
		Transformer transformer = transFactory.newTransformer();
		transformer.setOutputProperty("indent", "yes");
		DOMSource source=new DOMSource();
		source.setNode(doc);
		StreamResult result=new StreamResult();
		result.setOutputStream(new FileOutputStream(fileName));
		
		transformer.transform(source, result);
		
	}
	
	public static void modifyPomVersionByFile(String fileName, String express1, String express1Value,
			String express2, String express2Value) throws Exception {
		DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
		factory.setIgnoringElementContentWhitespace(true);
		DocumentBuilder documentBuilder = factory.newDocumentBuilder();
	
		Document xmldoc = documentBuilder.parse(new File(fileName));
		
		Element root = xmldoc.getDocumentElement();
		
		Element element1=(Element) selectSingleNode(express1, root);
		element1.setTextContent(express1Value);
		
		Element element2=(Element) selectSingleNode(express2, root);
		element2.setTextContent(express2Value);
		
		saveXml(fileName, xmldoc); 
	}
	
	public static void main(String[] args) throws Exception {
		DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
		factory.setIgnoringElementContentWhitespace(true);
		DocumentBuilder documentBuilder = factory.newDocumentBuilder();
		
		String fileName = "G:\\99无限\\20160607_ihealth_online\\root-3.7.0\\pom.xml";
		Document xmldoc=documentBuilder.parse(new File(fileName));
		
		Element root = xmldoc.getDocumentElement();
		Element theBook=(Element) selectSingleNode("/project/groupId", root);
		//--- 此时修改这本书的价格 ----- com.handpay
		//getElementsByTagName 返回的是NodeList，所以要跟上item(0)。另外，getElementsByTagName("price")相当于xpath 的".//price"。
//		theBook.getElementsByTagName("price").item(0).setTextContent("15");
		theBook.setTextContent("liguohao");
		saveXml(fileName, xmldoc); 
	}
}
